#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <limits>
#include <string>

// 扑克牌结构体
struct Card {
    int value; // 牌面值
    std::string name; // 牌名，如"2", "A", "J", "K"
};

// 初始化一副牌
std::vector<Card> initializeDeck() {
    std::vector<Card> deck;
    std::string names[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
    int values[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };

    for (int i = 0; i < 13; ++i) {
        for (int j = 0; j < 4; ++j) {
            Card card;
            card.value = values[i];
            card.name = names[i];
            deck.push_back(card);
        }
    }

    return deck;
}

// 洗牌
void shuffleDeck(std::vector<Card>& deck) {
    std::random_device rd;
    std::mt19937 rng(rd());
    std::shuffle(deck.begin(), deck.end(), rng);
}

// 发牌
void dealCards(const std::vector<Card>& deck, std::vector<Card>& player1, std::vector<Card>& player2, std::vector<Card>& player3) {
    for (int i = 0; i < 17; ++i) {
        player1.push_back(deck[i]);
        player2.push_back(deck[i + 17]);
        player3.push_back(deck[i + 34]);
    }
}

// 打印玩家手牌
void printHand(const std::vector<Card>& hand) {
    for (const auto& card : hand) {
        std::cout << card.name << " ";
    }
    std::cout << std::endl;
}

// 排序玩家手牌
void sortHand(std::vector<Card>& hand) {
    std::sort(hand.begin(), hand.end(), [](const Card& a, const Card& b) {
        return a.value < b.value;
        });
}

int main() {
    std::vector<Card> deck = initializeDeck();

    while (true) {
        std::cout << "Press Enter to deal cards (or 'q' to quit): ";

        // 等待用户输入回车键
        std::string input;
        std::getline(std::cin, input);

        if (input == "q") {
            break;  // 用户输入 'q' 退出循环
        }

        shuffleDeck(deck);

        std::vector<Card> player1, player2, player3;
        dealCards(deck, player1, player2, player3);

        // 对玩家手牌排序
        sortHand(player1);
        sortHand(player2);
        sortHand(player3);

        std::cout << "Player 1's hand: ";
        printHand(player1);

        std::cout << "Player 2's hand: ";
        printHand(player2);

        std::cout << "Player 3's hand: ";
        printHand(player3);

        // 清空输入缓冲区
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    return 0;
}
